﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bussiness;

namespace WebApplication.Admin
{
    public partial class ThueAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadHienthi();
            }
        }
        public void LoadHienthi()
        {
            IUnitOfWork iunitfwork = new EFUnitOfWork();
            THUERepository repository = new THUERepository(new EFRepository<THUE>(), iunitfwork);
            THUE objthue = null;
            string strid = "" + Request["MATH"];
            if (!string.IsNullOrEmpty(strid))
            {
                objthue = repository.GetById(strid);
                txtmathue.Value = objthue.MATH;
                txtsothue.Value = objthue.SOTHUE.ToString();
            }
        }
        public void capnhap()
        {
            IUnitOfWork iunitfwork = new EFUnitOfWork();
            THUERepository repository = new THUERepository(new EFRepository<THUE>(), iunitfwork);
            THUE objthue = null;
            string strid = "" + Request["MATH"];
            bool iscapnhap = true;
            if (!string.IsNullOrEmpty(strid))
            {
                iscapnhap = true;
                objthue = repository.GetById(strid);
            }
            else
            {
                objthue = new THUE();
            }
            if (string.IsNullOrEmpty(txtmathue.Value) || string.IsNullOrEmpty(txtsothue.Value))
            {
                RegisterClientScriptBlock("Thông báo", "<script> alert('Vui lòng nhập đầy đủ')</script>");
                return;
            }
            objthue.MATH = txtmathue.Value;
            objthue.SOTHUE = txtmathue.Value;
            try
            {
                if (iscapnhap)
                {
                    repository.Add(objthue);
                }
                repository.Save();
            }
            catch(Exception ex)
            {
                RegisterClientScriptBlock("Thông báo", "<script> alert('Lỗi rồi')</script>");
                return;
            }
            
        }
        protected void lbtncapnhap_Click(object sender, EventArgs e)
        {
            capnhap();
        }

        protected void lbtnthemmoi_Click(object sender, EventArgs e)
        {
            txtmathue.Value = "";
            txtsothue.Value = "";
            Response.Redirect("ThueAdd.aspx");
        }
    }
}